﻿package util {
	public class TextConvert {
		public function TextParser():void {
		}
		
		public static function inputHtmlToNormalHtml(s:String):String {
			//先将flash内置的textformat删除掉
			//s = s.replace(/\<\/?TEXTFORMAT[^\>]*\>/g, "");
			
			//将p替换成div
			/*s = s.replace(/\<P(.*?\>)(.*?)\<\/P\>/g, function() {
				return "<div" + arguments[1].toLowerCase() + arguments[2] + "</div>";
			});
			
			//将font标签改为小写，并删除letterspacing和kerning标签
			var font:Array = new Array();
			s = s.replace(/\<\/?FONT([^\>]*)\>([^\<]+)/g, function() {
				if (arguments[0].charAt(1) == "/") {
					font.pop();
				}
				else {
					font.push("<font" + arguments[1].toLowerCase().replace(/\s*letterspacing\=\"0\" kerning\=\"0\"/, "") + ">");
				}
				return font[font.length - 1] + arguments[2] + "</font>";
			});
			s = s.replace(/\<\/FONT\>/g, "");*/
			
			//格式化html标签，增加空格缩进使得易读
			s = s.replace(/\>\</g, ">\r<");
			
			return s;
		}
		
		public static function normalHtmlToInputHtml(s:String):String {
			//先移除html符号><之间多余的空白和=号两边的空格
			s = s.replace(/\>\s*\</g, "><").replace(/\>\r*/g, ">").replace(/\s+\=\s+/g, "");
			
			//将p替换成div
			/*s = s.replace(/\<div([^\>]*)\>/g, function() {
				return "<P" + arguments[1].toUpperCase() + ">";
			});
			s = s.replace(/\<\/div\>/g, "</P>");
			
			//将font标签大写，并加上letterspacing和kerning标签(fp自动)
			s = s.replace(/\<font([^\>]*)\>/g, function() {
				return "<FONT" + arguments[1].toUpperCase() + ">";
			});
			s = s.replace(/\<\/font\>/g, "</FONT>");*/
			
			return s;
		}
		
		/*public static function normalHtmlToUbb(s:String):String {
			//先移除html符号><之间多余的空格和=号两边的空格
			s = s.replace(/\>\s*\</g, "><").replace(/\s+\=\s+/g, "");
			
			//分析div的align属性
			s = s.replace(/\<div(.*?\>)(.*?)\<\/div\>/g, function() {
				var res:Array = new Array();
				//align取值默认left，center和right设置ubb标签
				var i:int = arguments[1].search(/align\=\"\w+\"/);
				if (i > -1) {
					var align:String = arguments[1].slice(i + 7, arguments[1].indexOf("\"", i + 7));
					if (align == "left") {
						res.push(arguments[2]);
					}
					else {
						res.push("[" + align.toUpperCase() + "]");
						res.push(arguments[2]);
						res.push("[/" + align.toUpperCase() + "]");
					}
				}
				else {
					res.push(arguments[2]);
				}
				return res.join("") + "\r";
			});
			
			//分析font的face、size、color属性
			s = s.replace(/\<font(.*?\>)(.*?)\<\/font\>/g, function () {
				var res:Array = new Array();
				//face默认为宋体，其它设置ubb标签
				var i:int = arguments[1].search(/face\=\".*?\"/);
				if (i > -1) {
					var face:String = arguments[1].slice(i + 6, arguments[1].indexOf("\"", i + 6));
					if (face != "宋体") {
						res.push("[FACE=" + face + "]");
						i = -2;
					}
				}
				//size默认12px，其它设置ubb标签
				var j:int = arguments[1].search(/size\=\"\d+\"/);
				if (j > -1) {
					var size:String = arguments[1].slice(j + 6, arguments[1].indexOf("\"", j + 6));
					if (size != "12") {
						res.push("[SIZE=" + size + "]");
						j = -2;
					}
				}
				//color默认#000000，其它设置ubb标签
				var k:int = arguments[1].search(/color\=\"#\w+\"/);
				if (k > -1) {
					var color:String = arguments[1].slice(k + 7, arguments[1].indexOf("\"", k + 7));
					if (color != "#000000") {
						res.push("[COLOR=" + color + "]");
						k = -2;
					}
				}
				res.push(arguments[2]);
				//ubb标签关门
				if (k == -2) {
					res.push("[/COLOR]");
				}
				if (j == -2) {
					res.push("[/SIZE]");
				}
				if (i == -2) {
					res.push("[/FACE]");
				}
				return res.join("");
			});
			
			return s;
		}
		
		public static function textToUbb(s:String):String {
			return normalHtmlToUbb(inputHtmlToNormalHtml(s));
		}
		
		public static function ubbToNormalHtml(s:String):String {
			var res:Array = new Array();
			
			//以换行符分割为数组，每个数组为一个div，单独处理每个div
			var array:Array = s.split("\r");
			for (var i:int = 0; i < array.length - 1; i++) {
				if (array[i] == "") {
					res.push("<div align=\"left\">&nbsp;</div>");
					continue;
				}
				var tempArr:Array = new Array();
				//替换align
				if (array[i].indexOf("[CENTER]") == 0) {
					tempArr.push("<div align=\"center\">");
					array[i] = array[i].slice(8, array[i].length - 8);
				}
				else if(array[i].indexOf("[RIGHT]") == 0) {
					tempArr.push("<div align=\"right\">");
					array[i] = array[i].slice(7, array[i].length - 7);
				}
				else {
					tempArr.push("<div align=\"left\">");
				}
				//font，先解析头尾
				array[i] = array[i].replace(/^(.+?)(\[COLOR|FACE|SIZE\=\w+?\])/, function() {
					return "<font face=\"宋体\" size=\"12\" color=\"#000000\">" + arguments[1] + "</font>" + arguments[2];
				});
				array[i] = array[i].replace(/(.+\[\/(COLOR|FACE|SIZE)\])(.+)$/, function() {
					return arguments[1] + "<font face=\"宋体\" size=\"12\" color=\"#000000\">" + arguments[3] + "</font>";
				});
				array[i] = array[i].replace(/\[(COLOR|FACE|SIZE)\=([^\]]+)\](.*?)\[\/\1\]/g, function() {
					return "<font " + arguments[1].toLowerCase() + "=\"" + arguments[2] + "\">" + arguments[3] + "</font>";
				});
				tempArr.push(array[i]);
				tempArr.push("</div>");
				res.push(tempArr.join(""));
			}
			
			return res.join("");
		}*/
	}
}